#include"BlockQueue.hpp"
#include"task.hpp"

#include<ctime>
#include<pthread.h>
#include<unistd.h>
#include<string>

void* Productor(void* arg) {

//  BlockQueue<int>* bq = (BlockQueue<int>*)arg;
//  while(true) {
//    sleep(1);
//    int data = rand() % 100 + 1;
//    bq->PushData(data);
//    std::cout << "Productor:" << data << std::endl;
//  }
//

  BlockQueue<Task>* bq = (BlockQueue<Task>*)arg;
  const std::string ops = "+-*/%";
  while(true) {
    int x = rand() % 100 + 1;
    int y = rand() % 100 + 1;
    char op = ops[rand() % 5];
    Task t(x, y, op);
    bq->PushData(t);
    std::cout << "producotr is already done\n";
  }

}

void* Consumer(void* arg) {
//  BlockQueue<int>* bq = (BlockQueue<int>*)arg;
//  while(true) {
//    int data;
//    bq->PopData(data);
//    std::cout << "Consumer:" << data << std::endl;
//  }
//

  BlockQueue<Task>* bq = (BlockQueue<Task>*)arg;
  while(true) {
    sleep(1);
    Task t;
    bq->PopData(t);
    t.Run();

  }
}

int main() {
  
  srand((unsigned int)time(nullptr));
  
//  pthread_t consumer, producotr;
//  BlockQueue<int>* bq = new BlockQueue<int>;
//    
//  pthread_create(&producotr, nullptr, Productor, bq);
//  pthread_create(&consumer, nullptr, Consumer, bq);
//
//  pthread_join(producotr, nullptr);
//  pthread_join(consumer, nullptr);
//  delete bq;
//
  

  pthread_t consumer, producotr;
  BlockQueue<Task>* bq = new BlockQueue<Task>;
    
  pthread_create(&producotr, nullptr, Productor, bq);
  pthread_create(&consumer, nullptr, Consumer, bq);

  pthread_join(producotr, nullptr);
  pthread_join(consumer, nullptr);
  delete bq;


  return 0;
}
